package com.gan.simple_init.controller;

import com.alibaba.excel.EasyExcel;
import com.gan.simple_init.common.base.ResponseResult;
import com.gan.simple_init.common.base.RestResultGenerator;
import com.gan.simple_init.common.easyexcellistner.MovieIgnoreFailListener;
import com.gan.simple_init.common.easyexcellistner.MovieNotIgnoreFailListener;
import com.gan.simple_init.model.movie.dto.AddMovieDto;
import com.gan.simple_init.model.movie.entity.Movie;
import com.gan.simple_init.service.MovieService;
import com.gan.simple_init.util.UserCacheUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;

/**
 * @author gbw
 * @date 2023/10/25
 */
@RestController
@RequestMapping("/movie")
@Slf4j
public class MovieController {

    @Resource
    MovieService movieService;

    /**
     * 新增电影
     *
     * @param addMovieDto
     * @return
     */
    @ApiOperation("新增电影")
    @PostMapping("/addMovie")
    public ResponseResult<?> addMovie(@RequestBody @Valid AddMovieDto addMovieDto) {
        Movie movie = new Movie();
        BeanUtils.copyProperties(addMovieDto, movie);
        String validInfo = movieService.validMovieInfo(movie);
        if (validInfo != null) {
            return RestResultGenerator.error(validInfo);
        }
        movie.setCreateBy(UserCacheUtil.getUserId().toString());
        movieService.save(movie);
        return RestResultGenerator.success("新增电影成功!");
    }

    // todo 删除、修改、分页返回、用户发表影评【影评的发布，删除，修改，点赞，取消点赞】

    /**
     * 通过 Excel批量导入电影，有失败时不会停止上传数据
     *
     * @param movieExcel
     * @return
     */
    @ApiOperation("通过 Excel批量导入电影，有失败时不会停止上传数据")
    @PostMapping("/uploadMovieIgnoreFail")
    public ResponseResult<?> uploadMovieIgnoreFail(@RequestPart MultipartFile movieExcel) throws IOException {
        MovieIgnoreFailListener movieIgnoreFailListener = new MovieIgnoreFailListener(movieService);
        // 这里 需要指定读用哪个class去读，然后读取第一个sheet
        EasyExcel.read(movieExcel.getInputStream(), AddMovieDto.class, movieIgnoreFailListener)
                .sheet(0)
                // 从第几行开始读，不使用自定义样式的时候不用指定【默认从0开始】
                .headRowNumber(2)
                .doRead();
        String res = movieIgnoreFailListener.failedNum == 0 ?
                        "上传成功" + movieIgnoreFailListener.successNum + "条，" +
                        "上传失败" + movieIgnoreFailListener.failedNum + "条，"
                         :
                        "上传成功" + movieIgnoreFailListener.successNum + "条，" +
                        "上传失败" + movieIgnoreFailListener.failedNum + "条，" +
                        "具体失败信息为：" + movieIgnoreFailListener.failedInfo;
        return RestResultGenerator.success(res);
    }

    /**
     * 通过 Excel批量导入电影，有失败的就不上传任何数据，返回所有错误信息
     *
     * @param movieExcel
     * @return
     */
    @ApiOperation("通过 Excel批量导入电影，有失败时会停止上传数据")
    @PostMapping("/uploadMovieNotIgnoreFail")
    public ResponseResult<?> uploadMovieNotIgnoreFail(@RequestPart MultipartFile movieExcel) throws IOException {
        MovieNotIgnoreFailListener movieNotIgnoreFailListener = new MovieNotIgnoreFailListener(movieService);
        // 这里 需要指定读用哪个class去读，然后读取第一个sheet
        EasyExcel.read(movieExcel.getInputStream(), AddMovieDto.class, movieNotIgnoreFailListener)
                .sheet(0)
                // 从第几行开始读，不使用自定义样式的时候不用指定【默认从0开始】
                .headRowNumber(2)
                .doRead();
        String res = movieNotIgnoreFailListener.failedNum == 0 ?
                "上传成功!"
                :
                "上传失败!" + "具体失败信息为：" + movieNotIgnoreFailListener.failedInfo;
        return RestResultGenerator.success(res);
    }

    /**
     * 下载Excel导入模板
     *
     * @param response
     * @return
     */
    @ApiOperation("下载导入模板")
    @GetMapping("/downloadTemplate")
    public void downloadTemplate(HttpServletResponse response) throws IOException {
        movieService.downloadTemplate(response);
    }

    /**
     * 导出数据
     *
     * @param response
     * @return
     */
    @ApiOperation("导出数据")
    @GetMapping("/downloadData")
    public void downloadData(HttpServletResponse response) throws IOException {
        movieService.downloadData(response);
    }


}
